import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.fileUrl + '/v1' + '/minio/cloud_'
/**
 * 获取云端文件列表
 * path 文件路径
 * **/
export function getCloudList (path) {
  const newUrl = baseUrl + 'list'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取OSS临时授权
 * **/
export function getOSSAccount (path) {
  const newUrl = baseUrl + 'tst'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 重命名文件
 * newKey 新路径
 * oldKey 旧路径
 * **/
export function setNewName (params) {
  const newUrl = baseUrl + 'rename'
  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 新建文件夹
 * path:创建文件夹的路径
 **/
export function newDir (path) {
  const newUrl = baseUrl + 'createfolder'
  const params = {
    path
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 新建文件
 * content 文件内容
 * dir 文件夹的路径
 * fileName 文件名称
 **/
export function newFile (params) {
  const newUrl = baseUrl + 'create'

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 读取文件内容
 * key 文件全路径
 * **/
export function readFile (key) {
  const newUrl = baseUrl + 'open'

  const params = {
    key
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 编辑文件
 * content 文件内容
 * key 文件全路径
 **/
export function editFile (params) {
  const newUrl = baseUrl + 'edit'

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// 删除
/**
 * 删除文件夹
 * path 删除路径
 **/
export function rmDir (path) {
  const newUrl = baseUrl + 'folder_to_recyclebin'
  const params = {
    key: path
  }

  return axiosInstance.delete(newUrl, { params: params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除文件
 * key 删除路径
 **/
export function rmFile (key) {
  const newUrl = baseUrl + 'delete'
  const params = {
    key
  }

  return axiosInstance.delete(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除文件到回收站
 * key 删除路径
 * size 文件大小
 **/
export function rmFileToRecyclebin (key, size) {
  const newUrl = baseUrl + 'file_to_recyclebin'
  const params = {
    key,
    size
  }

  return axiosInstance.delete(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 批量删除文件和文件夹
 * list path，isFile
 **/
export function batchFileDir (params) {
  const newUrl = baseUrl + 'batchdelete'

  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取回收站内容列表
 * path 路径
 **/
export function getCloudRecyclebin (path) {
  const newUrl = baseUrl + 'list_recyclebin'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 还原回收站内容
 * key id
 * realname 原路径
 **/
export function restoreRecyclebin (params) {
  const newUrl = baseUrl + 'file_resume'

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

// 传输记录
/**
 * 获取传输记录
 **/
export function getProgressRecord () {
  const newUrl = baseUrl + 'record_list'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 增加传输记录
 * fileName 文件名称
 * sender 发送方
 * reciver 接受放
 * reciverPaht 接受路径
 * startTime 开始时间
 * endTime 结束时间
 * useTime 累计用时
 **/
export function addProgressRecord (params) {
  const newUrl = baseUrl + 'record_add'

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除一条传输记录
 * id 记录id
 **/
export function delProgressRecord (id) {
  const newUrl = baseUrl + 'record_delete'
  const params = {
    id
  }

  return axiosInstance.delete(newUrl, { params: params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 清空删除传输记录
 * id 记录id
 **/
export function clearProgressRecord () {
  const newUrl = baseUrl + 'record_clear'

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取文件历史版本列表
 * key 文件全路径
 **/
export function getVersionList (key) {
  const newUrl = baseUrl + 'version_list'
  let params = {
    key
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 切换版本
 * realpath 文件全路径
 * version 版本名称
 **/
export function changeVersion (params) {
  const newUrl = baseUrl + 'version_back'

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 读取版本内容
 * version 版本名称
 **/
export function readFileVersionContent (version) {
  const newUrl = baseUrl + 'version_read'
  const params = {
    version
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 检查文件是否存在
 * key 文件全路径
 **/
export function cloudFileExist (key) {
  const newUrl = baseUrl + 'check'
  const params = {
    key
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

export function getUploadRecord (params) {
  const newUrl = baseUrl + `specify_path_upload/record?cloudPath=${encodeURIComponent(params.cloudPath)}`
  let data = new FormData()
  data.append('file', params.file)
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve((res))
  })
}
// 文件下载
export function getDownloadRecord (params) {
  const newUrl = baseUrl + `specify_path_download?cloudPath=${encodeURIComponent(params.cloudPath)}`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 获取文件服务器地址
export function getBasisUrl () {
  const newUrl = `${CommonConfig.fileUrl}/v1/minio/get_basis_url`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
